﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.NewProductBigPic.Properties;

namespace MINERP.NewProductBigPic
{
    public partial class FrmSettings : Form
    {

        List<BarSerInfo> _listR;

        public FrmSettings()
        {
            InitializeComponent();
        }

        private void FrmSettings_Load(object sender, EventArgs e)
        {
            //loadDefaultValue
            LoadDefaultValue();
        }

        private void LoadDefaultValue()
        {
            txtFontSize.Text = Settings.Default.FontSize.ToString();
            txtSamllSizeToPrintBarCode.Text = Settings.Default.SmallSizeToPrintBarCode.ToString();
            //checkBoxBackColorIsGreen.Checked = Settings.Default.BackColor;
            colorBoardBackColor.Color = Settings.Default.BackColor;
            chkAddIccByPS.Checked = Settings.Default.AddIccByPs;
            chkSetColorSpace.Checked = Settings.Default.SetColorSpace;
            radioLeftAndRight.EditValue = Settings.Default.BarHDirection;//横向
            radioUpAndBottom.EditValue = Settings.Default.BarVDirection;//纵向
            txtBarCodeSize.Text = Settings.Default.BarCodeSize.ToString();//条码大小
            txtColorSpace.Text = Settings.Default.ColorSpace;//色彩空间
            txtMakeBigpictureNo.Text = Settings.Default.MakeBigpictureNo.ToString();
            checkBoxAutoCute.Checked = Settings.Default.IsAutoCut;
            textBoxMinAutoNum.Text = Settings.Default.MinAutoCut.ToString();
            checkBoxTiaoseNo.Checked = Settings.Default.TiaoseNo;
            checkBoxTextInPhoto.Checked = Settings.Default.BoolPrintInphoto;
            textBoxSmallPhotosize.Text = Settings.Default.SmallPhotosize.ToString();

            _listR = GetInitil();
              Bleed bleed = DateTransfer.CurrentBleed;

              comboBoxBleed.SelectedIndex = bleed.Type;
              textEditMergenMM.Text = bleed.Value.ToString();
            if (comboBoxBleed.SelectedIndex == 0)
            {
                labelBleedUnit.Text = "mm";
            }
            else
            {
                labelBleedUnit.Text = "%";
            }
            gridControl1.DataSource = _listR;
        }


        /// <summary>
        /// 从字符串中解析相应的条码的打印的信息
        /// </summary>
        /// <returns></returns>
        public static List<BarSerInfo> GetInitil()
        {
            List<BarSerInfo> listR = new List<BarSerInfo>();
            string barSer = Settings.Default.BarSetting;
            if (string.IsNullOrEmpty(barSer))
            {
                barSer = "1_GN_分片格子号_1,2_PID_照片编号_1,3_OID_定单编号_1,4_PREMARK_照片备注_1,5_ONAME_定单名称_1,6_CName_客人名称_1,7_SNAME_商户名称_1,8_BID_大板编号_1,9_PSIZE_尺寸信息_1,10_DATE_打印时间_1,11_PNAME_照片名_1,12_NumInOrder_照片张数_1";
                Settings.Default.BarSetting=barSer;
                Settings.Default.Save();

            }
            foreach (var barSet in barSer.Split(new char[] { ',' }))
            {
                //index_code_des_isprint
                var set = barSet.Split(new char[] { '_' });
                listR.Add(new BarSerInfo()
                   {
                       BarIndex = Int32.Parse(set[0]),
                       Code = set[1],
                       Description = set[2],
                       IsPrint = set[3] == "1" ? true : false
                   });
            }
            listR = listR.OrderBy(s => s.BarIndex).ToList();

            return listR;
        }

        public class BarSerInfo
        {
            public int BarIndex { get; set; }//显示的顺序
            public string Code { get; set; }//代码
            public string Description { get; set; }
            public bool IsPrint { get; set; }//是否打印
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //保存修改的值
            int FontSize, SmallSizeToPrintBarCode,MakeBigpictureNo;
            string ColorSpace = null;
            try
            {
                FontSize = Convert.ToInt32(txtFontSize.Text.Trim());
                SmallSizeToPrintBarCode = Convert.ToInt32(txtSamllSizeToPrintBarCode.Text.Trim());
                MakeBigpictureNo = Convert.ToInt32(txtMakeBigpictureNo.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("请输入正确的数字");
                return;
            }

            if (Convert.ToInt32(txtMakeBigpictureNo.Text.Trim()) <= 100)
            {
                MessageBox.Show("生成器编号必须是101-999之间的整数");
                txtMakeBigpictureNo.Focus();
                return;
            }

            if (FontSize < 2 || SmallSizeToPrintBarCode < 0)
            {
                MessageBox.Show("输出的字体大小不能小于10,最小的尺寸数不能小于0");
                return;
            }

            Settings.Default.AddIccByPs = chkAddIccByPS.Checked;
            //Settings.Default.BackColor = checkBoxBackColorIsGreen.Checked;
            Settings.Default.BackColor = colorBoardBackColor.Color;
            Settings.Default.SetColorSpace = chkSetColorSpace.Checked;
            Settings.Default.FontSize = FontSize;
            Settings.Default.SmallSizeToPrintBarCode = SmallSizeToPrintBarCode;
            Settings.Default.BarCodeSize = Convert.ToInt32(txtBarCodeSize.Text.Trim());
            Settings.Default.IsAutoCut = Convert.ToBoolean(checkBoxAutoCute.Checked);

            Settings.Default.BarHDirection = Convert.ToBoolean(this.radioLeftAndRight.EditValue);//横向
            Settings.Default.BarVDirection = Convert.ToBoolean(radioUpAndBottom.EditValue);//纵向
            Settings.Default.MakeBigpictureNo = Convert.ToInt32(txtMakeBigpictureNo.Text.Trim());//生成大图器编号
            Settings.Default.MinAutoCut = Convert.ToInt16(textBoxMinAutoNum.Text);
            Settings.Default.BoolPrintInphoto = checkBoxTextInPhoto.Checked;
            Settings.Default.SmallPhotosize = Convert.ToInt32(textBoxSmallPhotosize.Text);


             int bLInt = comboBoxBleed.SelectedIndex;
            Double preserntDo = Convert.ToDouble(textEditMergenMM.Text);
            Bleed sBleed = new Bleed(bLInt, preserntDo);
            DateTransfer.CurrentBleed = sBleed;
           
            if(!string.IsNullOrEmpty( txtColorSpace.Text.Trim()))
            {
                Settings.Default.ColorSpace = txtColorSpace.Text.Trim();
            }
            SaveBarSerToSetting();

            Settings.Default.Save();
            MessageBox.Show("设置成功！须重新启动生成大图，此设置才会生效！");
            this.Close();
        }

        /// <summary>
        /// 根据用户的调整将条码顺序的信息写入到setting中
        /// </summary>
        private void SaveBarSerToSetting()
        {
            List<BarSerInfo> listR = gridControl1.DataSource as List<BarSerInfo>;
            if (listR != null)
            {
                StringBuilder sbR = new StringBuilder();
                foreach (var barSet in listR)//
                {
                    sbR.Append(barSet.BarIndex + "_" + barSet.Code + "_" + barSet.Description + "_" + Convert.ToInt32(barSet.IsPrint) + ",");
                }
                sbR.Remove(sbR.Length - 1, 1);//去除最后一个","

                Settings.Default.BarSetting = sbR.ToString();
            }

        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            //将选中的项提向上
            BarSerInfo selBarSet = gridView1.GetFocusedRow() as BarSerInfo;
            if (selBarSet != null)
            {
                if (selBarSet.BarIndex > 1)
                {
                    _listR.FirstOrDefault(s => s.BarIndex == selBarSet.BarIndex - 1).BarIndex = selBarSet.BarIndex;
                    selBarSet.BarIndex -= 1;
                    _listR = _listR.OrderBy(s => s.BarIndex).ToList();
                    gridControl1.DataSource = _listR;
                    gridControl1.RefreshDataSource();
                    gridView1.SelectRow(selBarSet.BarIndex - 1);
                    gridView1.FocusedRowHandle = selBarSet.BarIndex - 1;
                }
                else
                {
                    MessageBox.Show("已是最前面了，不能再向前!");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //将选中的项提向下
            BarSerInfo selBarSet = gridView1.GetFocusedRow() as BarSerInfo;
            if (selBarSet != null)
            {
                if (selBarSet.BarIndex < _listR.Count)
                {
                    _listR.FirstOrDefault(s => s.BarIndex == selBarSet.BarIndex + 1).BarIndex = selBarSet.BarIndex;
                    selBarSet.BarIndex += 1;
                    _listR = _listR.OrderBy(s => s.BarIndex).ToList();
                    gridControl1.DataSource = _listR;
                    gridControl1.RefreshDataSource();
                    gridView1.SelectRow(selBarSet.BarIndex-1);
                    gridView1.FocusedRowHandle = selBarSet.BarIndex-1;
                }
                else
                {
                    MessageBox.Show("已是最后了，不能再向后!");
                }
            }
        }

        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                BarSerInfo bs = gridView1.GetRow(e.RowHandle) as BarSerInfo;
                if (bs != null)
                {
                    e.Appearance.BackColor = bs.IsPrint ? Color.White : Color.Gray;
                }
            }

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void colorBoardBackColor_ColorChanged(object sender, EventArgs e)
        {
            Settings.Default.BackColor = colorBoardBackColor.Color;
            Settings.Default.Save();
        }

        private void textBoxMinAutoNum_TextChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxTiaoseNo_CheckedChanged(object sender, EventArgs e)
        {
            Settings.Default.TiaoseNo = checkBoxTiaoseNo.Checked;
            Settings.Default.Save();
        }

        private void chkSetColorSpace_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBoxPrintLine_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.BoolPrintPhotoLine = checkBoxPrintLine.Checked;
            Properties.Settings.Default.Save();
        }

        private void comboBoxBleed_DropDownClosed(object sender, EventArgs e)
        {
            if (comboBoxBleed.SelectedIndex == 0)
            {
                labelBleedUnit.Text = "mm";
            }
            else
            {
                labelBleedUnit.Text = "%";
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    
    }
}
